package com.gmail.jaredjeya.awesomeplatformer.model;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Configuration;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.gmail.jaredjeya.awesomeplatformer.Constants;

public class InputHandler {

	private int		sizeX, sizeY;
	private int		leftL, leftR;
	private int		rightL, rightR;
	private int		shootL, shootR;
	private int		jumpL, jumpR;
	private int		buttonT, buttonB;

	private boolean[]	keys;
	public boolean hardwareKeyboard, dPad, numericKeyboard;

	private ArrayList<Float>	fingerX, fingerY;
	public boolean				leftPressed	= false, rightPressed = false, shootPressed = false, jumpPressed = false;
	

	public InputHandler(Context context) {
		fingerX = new ArrayList<Float>();
		fingerY = new ArrayList<Float>();
		keys = new boolean[65536];
		
		switch (context.getResources().getConfiguration().keyboard) {
			case Configuration.KEYBOARD_QWERTY:
				hardwareKeyboard = true;
				numericKeyboard = false;
				break;
			case Configuration.KEYBOARD_12KEY:
				hardwareKeyboard = false;
				numericKeyboard = true;
				break;
			default:
				hardwareKeyboard = false;
				numericKeyboard = false;
				break;
		}
		
		switch (context.getResources().getConfiguration().navigation) {
			case Configuration.NAVIGATION_DPAD:
				dPad = true;
				break;
			default:
				dPad = false;
				break;
		}
	}

	public void update() {
		boolean leftPressedTemp = false;
		boolean rightPressedTemp = false;
		boolean shootPressedTemp = false;
		boolean jumpPressedTemp = false;
		for (int i = 0; i < fingerX.size() && i < fingerY.size(); i++) {
			if (fingerY.get(i) > buttonT && fingerY.get(i) < buttonB) {
				if (fingerX.get(i) > leftL && fingerX.get(i) < leftR) leftPressedTemp = true;
				if (fingerX.get(i) > rightL && fingerX.get(i) < rightR) rightPressedTemp = true;
				if (fingerX.get(i) > shootL && fingerX.get(i) < shootR) shootPressedTemp = true;
				if (fingerX.get(i) > jumpL && fingerX.get(i) < jumpR) jumpPressedTemp = true;
			}
		}
		
		leftPressed = leftPressedTemp || keys[KeyEvent.KEYCODE_DPAD_LEFT] || keys[KeyEvent.KEYCODE_A] || keys[KeyEvent.KEYCODE_NUMPAD_2];
		rightPressed = rightPressedTemp || keys[KeyEvent.KEYCODE_DPAD_RIGHT] || keys[KeyEvent.KEYCODE_D] || keys[KeyEvent.KEYCODE_NUMPAD_6];
		jumpPressed = jumpPressedTemp || keys[KeyEvent.KEYCODE_DPAD_UP] || keys[KeyEvent.KEYCODE_W] || keys[KeyEvent.KEYCODE_NUMPAD_4];
		shootPressed = shootPressedTemp || keys[KeyEvent.KEYCODE_DPAD_CENTER] || keys[KeyEvent.KEYCODE_SPACE] || keys[KeyEvent.KEYCODE_NUMPAD_5];

	}

	public void setSize(int sizeX, int sizeY) {
		this.sizeX = sizeX;
		this.sizeY = sizeY;
		buttonB = this.sizeY - Constants.BUTTON_SEPARATION;
		buttonT = this.sizeY - (Constants.BUTTON_SIZE + Constants.BUTTON_SEPARATION);
		leftL = Constants.BUTTON_SEPARATION;
		leftR = Constants.BUTTON_SEPARATION + Constants.BUTTON_SIZE;
		rightL = 2 * Constants.BUTTON_SEPARATION + Constants.BUTTON_SIZE;
		rightR = 2 * Constants.BUTTON_SEPARATION + 2 * Constants.BUTTON_SIZE;
		shootL = this.sizeX - (2 * Constants.BUTTON_SEPARATION + 2 * Constants.BUTTON_SIZE);
		shootR = this.sizeX - (2 * Constants.BUTTON_SEPARATION + Constants.BUTTON_SIZE);
		jumpL = this.sizeX - (Constants.BUTTON_SEPARATION + Constants.BUTTON_SIZE);
		jumpR = this.sizeX - Constants.BUTTON_SEPARATION;
	}

	public void touch(MotionEvent event) {
		int index = event.getActionIndex();
		switch (event.getActionMasked()) {
			case MotionEvent.ACTION_DOWN:
				fingerX.add(0, event.getX(0));
				fingerY.add(0, event.getY(0));
				// Log.v(Constants.DEBUG_TAG, String.format("First Finger Down; X: %f Y: %f", event.getX(index), event.getY(index)));
				break;
			case MotionEvent.ACTION_POINTER_DOWN:
				fingerX.add(index, event.getX(index));
				fingerY.add(index, event.getY(index));
				// Log.v(Constants.DEBUG_TAG, String.format("Finger Down; %d X: %f Y: %f", index, event.getX(index), event.getY(index)));
				break;
			case MotionEvent.ACTION_UP:
				fingerX.clear();
				fingerY.clear();
				// Log.v(Constants.DEBUG_TAG, String.format("Final Finger Up; X: %f Y: %f", event.getX(index), event.getY(index)));
				break;
			case MotionEvent.ACTION_POINTER_UP:
				fingerX.remove(index);
				fingerY.remove(index);
				// Log.v(Constants.DEBUG_TAG, String.format("Finger Up; Finger: %d X: %f Y: %f", index, event.getX(index), event.getY(index)));
				break;
			case MotionEvent.ACTION_MOVE:
				fingerX.set(index, event.getX(index));
				fingerY.set(index, event.getY(index));
				// Log.v(Constants.DEBUG_TAG, String.format("Finger Moved; Finger: %d X: %f Y: %f", index, event.getX(index), event.getY(index)));
				break;
			default:
				// Log.v(Constants.DEBUG_TAG, String.format("Other Touch: %d Finger: %d", event.getAction(), index));
				break;
		}

	}

	public void key(int keyCode, KeyEvent event) {
		switch (event.getAction()) {
			case KeyEvent.ACTION_DOWN:
				keys[keyCode] = true;
				break;
			case KeyEvent.ACTION_UP:
				keys[keyCode] = false;
				break;
		}
	}

}
